{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Summary\n",
    "In this notebook we will setup a *TSEB* model to run a single image using local meteorological data.\n",
    "\n",
    "\n",
    "# TSEB setup\n",
    "The main input data consists of any *GDAL* compatible raster image containing the radiometric temperature(s). Depending on the TSEB model to run the image should contain:\n",
    "\n",
    "- Priestley-Taylor TSEB (**TSEB-PT**): 1st band with the radiometric surface temperature (Kelvin)\n",
    "- Dual-Time Difference TSEB (**DTD**): 1st band with the radiometric surface temperature around noon. 2nd band with radiometric surface temperature around sunrise (Kelvin)\n",
    "- Composite temperatures TSEB( **TSEB-2T**): 1st band with  canopy temperature. 2nd band with soil temperature(Kelvin)\n",
    "\n",
    "You can add additional images for leaf area index, view zenith angle (degrees), fractional cover, canopy height (m), canopy width-to-height ratio, green fraction of vegetation, and a processing mask. For all these inputs, you can also type a constant value to be applied to the whole scene (i.e. use this constant value to run *TSEB* in all the pixels within the processing mask).\n",
    "\n",
    "For more information about the inputs and outpus of pyTSEB click [here](./README_Notebooks.md \"README_Notebooks.md\")\n",
    "\n",
    "## Select the input and output files and fill the information in all tabs \n",
    "You can press the *Load Configuration File* button to load a configuration text file which will upload all its information to the corresponding cells. \n",
    "\n",
    "You can also press the *Save Configuration File* button to save the configuration in a text file which can be used in future runs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyTSEB.TSEBIPythonInterface import TSEBIPythonInterface # import the PyTSEB class object in the pyTSEB.py module\n",
    "setup = TSEBIPythonInterface() # Create the setup instance from the PyTSEB class object\n",
    "setup.local_image_widget() # Load the TSEB configuration Widget"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Run TSEB\n",
    "Once TSEB is configured we parsed all the information in the widgets and run the selected model. \n",
    "\n",
    "After the processing is done two GeoTiff files will be saved:\n",
    "\n",
    "- < Main Output File > whose name is specified in the cell *Output File* will contain the bulk estimated fluxes:\n",
    "    1. Net radiation (W m-2)\n",
    "    2. Sensible heat flux (W m-2)\n",
    "    3. Latent heat flux (W m-2)\n",
    "    4. Soil heat flux (W m-2)\n",
    "\n",
    "\n",
    "- < Ancillary Output File > with the same name as the main input file but with a suffix *_ancillary* added, will contain ancillary information from TSEB:\n",
    "    1. Net shortwave radiation (W m-2)\n",
    "    2. Net longwave radiation (W m-2)\n",
    "    3. Canopy sensible heat flux (W m-2)\n",
    "    4. Canopy latent heat flux (W m-2)\n",
    "    5. Evapotrasnpiration partitioning (canopy LE/total LE)\n",
    "    6. Canopy temperature (K)\n",
    "    7. Soil temperature (K)\n",
    "    8. Aerodynamic resistance (s m-1)\n",
    "    9. Bulk canopy resistance to heat transport (s m-1)\n",
    "    10. Soil resistance to heat transport (s m-1)\n",
    "    11. Friction velocity (m s-1)\n",
    "    12. Monin-Obukhov lenght (m)\n",
    "    13. Friction velocity (m s-1)\n",
    "    14. Quality Flag (unitless)\n",
    " \n",
    "# Display results\n",
    "Now we can open the image and display the TSEB outputs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Change to have a different colour stretch\n",
    "high_flux=600 # Maximum flux value in the display\n",
    "low_flux=0 # Minimum flux value in the display\n",
    "\n",
    "from bokeh.plotting import *\n",
    "from bokeh.palettes import RdYlBu11 as colortable\n",
    "from bokeh.models.mappers import LinearColorMapper\n",
    "from bokeh.io import output_notebook\n",
    "import numpy as np\n",
    "import gdal\n",
    "from bokeh.resources import INLINE\n",
    "output_notebook(resources=INLINE)\n",
    "\n",
    "# Open the file\n",
    "fid=gdal.Open(setup.outputFile,gdal.GA_ReadOnly)\n",
    "rows=fid.RasterYSize\n",
    "cols=fid.RasterXSize\n",
    "# read each band and store the arrays\n",
    "H=fid.GetRasterBand(1).ReadAsArray()\n",
    "LE=fid.GetRasterBand(2).ReadAsArray()\n",
    "Rn=fid.GetRasterBand(3).ReadAsArray()\n",
    "G=fid.GetRasterBand(4).ReadAsArray()\n",
    "del fid\n",
    "colortable=list(reversed(colortable))\n",
    "map_LE=LinearColorMapper(palette=colortable,high=high_flux,low=low_flux)\n",
    "\n",
    "# Setup the figure\n",
    "s1= figure(title=\"H\",plot_width=cols, plot_height=rows, x_range=[0, cols], y_range=[0, rows])\n",
    "s1.axis.visible = None\n",
    "s1.image(image=[np.flipud(H)],x=[0],y=[0],dw=cols,dh=rows,color_mapper=map_LE)\n",
    "s2= figure(title=\"LE\",plot_width=cols, plot_height=rows, x_range=s1.x_range, y_range=s1.y_range)\n",
    "s2.axis.visible = None\n",
    "s2.image(image=[np.flipud(LE)],x=[0],y=[0],dw=[cols],dh=[rows],color_mapper=map_LE)\n",
    "s3= figure(title=\"Rn\",plot_width=cols, plot_height=rows, x_range=s1.x_range, y_range=s1.y_range)\n",
    "s3.image(image=[np.flipud(Rn)],x=[0],y=[0],dw=[cols],dh=[rows],color_mapper=map_LE)\n",
    "s3.axis.visible = None\n",
    "s4= figure(title=\"G\",plot_width=cols, plot_height=rows, x_range=s1.x_range, y_range=s1.y_range)\n",
    "s4.image(image=[np.flipud(G)],x=[0],y=[0],dw=[cols],dh=[rows],color_mapper=map_LE)\n",
    "s4.axis.visible = None\n",
    "p = gridplot([[s1, s2,s3,s4]], toolbar_location='above')\n",
    "\n",
    "# Add a colormap legend\n",
    "y = np.linspace(low_flux,high_flux,len(colortable))\n",
    "dy = y[1]-y[0]\n",
    "ramp = figure(tools=\"\", y_range = [0, 1], x_range = [low_flux,high_flux], plot_width = 650, plot_height=100)\n",
    "ramp.toolbar_location=None\n",
    "ramp.yaxis.visible = None\n",
    "ramp.rect(x=y, y=0.5, color=colortable, width=dy, height = 1)\n",
    "\n",
    "show(p)\n",
    "show(ramp);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
